#include "lib_fixed.h"

const char *fixed::show(int options) const
{
    fixed_numerator x = value;
    bool            negative;
    if (x < 0)
    {
        x = -x;
        // prevent buffer overflow if result is still negative
        if (x < 0)
            x = x - 1;
        negative = true;
    }
    else
        negative = false;
    int         n = 0;
    int         units;
    static char buffer[MAX_FIXED_LENGTH + MAX_FIXED_PRECISION];
    do
    {
        if (n == precision)
        {
            if (n > 0 || options & DECIMAL)
                buffer[sizeof(buffer) - ++n] = '.';
            units = n;
        }
        else if (options & COMMAS && n > precision && (n - units) % 4 == 3)
            buffer[sizeof(buffer) - ++n] = ',';
        fixed_numerator y;
        y                            = x / 10;
        buffer[sizeof(buffer) - ++n] = integer(x - y * 10) + '0';
        x                            = y;
    } while (n <= precision || x != 0);
    if (negative)
        buffer[sizeof(buffer) - ++n] = '-';
    if (options & ALIGN)
    {
        while (n - units < MAX_FIXED_LENGTH - 2)
            buffer[sizeof(buffer) - ++n] = ' ';
    }
    return (const char *) buffer + sizeof(buffer) - n;
    return ""; //inserted to get around linker bug!
}

